#include <reg51.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
sbit ENLED=P1^4;
sbit ADDR0=P1^0;
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
code uint8 number[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xa7,0xa1,0x86,0x8e};
uint8 a[6];
uint32 num;


void refresh()
{
	static uint8 j = 0;
	switch(j)
    {
		case 0: ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;j++;P0 = number[a[0]];break;
		case 1: ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;j++;P0 = number[a[1]];break;
		case 2: ADDR0 = 0;ADDR1 = 1;ADDR2 = 0;j++;P0 = number[a[2]];break;
		case 3: ADDR0 = 1;ADDR1 = 1;ADDR2 = 0;j++;P0 = number[a[3]];break;
		case 4: ADDR0 = 0;ADDR1 = 0;ADDR2 = 1;j++;P0 = number[a[4]];break;
		case 5: ADDR0 = 1;ADDR1 = 0;ADDR2 = 1;j = 0;P0 = number[a[5]];break;
		default: j=0;break;
	}
}

void light_num()
{
	a[0]=num%10;
	a[1]=(num/10)%10;
	a[2]=(num/100)%10;
	a[3]=(num/1000)%10;
	a[4]=(num/10000)%10;
	a[5]=(num/100000)%10;
}

void main()
{
	uint16 counter;
	ENLED=0;ADDR3=1;
	ADDR0=1;ADDR1=0;ADDR2=1;
	TMOD=0x01;
	TF0=0;TR0=1;
	TH0=0xfc;TL0=0x67;
	counter=0;
	num=0;
	while(1)
	{
		if(1==TF0)//12*(65536-x)/11059200=0.001
		{
			TF0=0;
			TH0=0xfc;TL0=0x67;
			counter++;
		}
		if(1000==counter)
		{
			num++;
			counter=0;
		}
		light_num();
		refresh();
	}
}